package marksys.free.com.marksys.ui.face.facebykomlinplus;

/***
 * 为了解决预览变形问题
 */


import android.hardware.Camera;

import java.util.List;


public class CamParaUtil {
	private static final String TAG = "yanzi";
	public static  Camera.Size getCloselyPreSize(boolean isPortrait, int surfaceWidth, int surfaceHeight, List<Camera.Size> preSizeList) {
		int reqTmpWidth;
		int reqTmpHeight;
		// 当屏幕为垂直的时候需要把宽高值进行调换，保证宽大于高
		if (isPortrait) {
			reqTmpWidth = surfaceHeight;
			reqTmpHeight = surfaceWidth;
		} else {
			reqTmpWidth = surfaceWidth;
			reqTmpHeight = surfaceHeight;
		}
		//先查找preview中是否存在与surfaceview相同宽高的尺寸
		for (Camera.Size size : preSizeList) {
			if ((size.width == reqTmpWidth) && (size.height == reqTmpHeight)) {
				return size;
			}
		}

		// 得到与传入的宽高比最接近的size
		float reqRatio = ((float) reqTmpWidth) / reqTmpHeight;
		float curRatio, deltaRatio;
		float deltaRatioMin = Float.MAX_VALUE;
		Camera.Size retSize = null;
		for (Camera.Size size : preSizeList) {
			curRatio = ((float) size.width) / size.height;
			deltaRatio = Math.abs(reqRatio - curRatio);
			if (deltaRatio < deltaRatioMin) {
				deltaRatioMin = deltaRatio;
				retSize = size;
			}
		}


		return retSize;

	}


}
